package com.flymobi.service;

import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.po.ResFile;
import com.flymobi.util.QL;

public class ResFileService extends CommonService {

	public List<ResFile> list(Long resId, int startRow, int pagePerSize) {
		return dao.find(ResFile.class, QL.HQL.ResFile.LIST,
				new Object[] { resId }, startRow, pagePerSize);
	}

	public List<ResFile> list(Long resId, Long idSeq, int startRow,
			int pagePerSize) {
		return dao.find(ResFile.class, QL.HQL.ResFile.LIST_ALL,
				new Object[] { resId, idSeq }, startRow, pagePerSize);
	}

	public Long getCount(Long newsId) {
		return dao
				.getCount(QL.HQL.ResFile.GET_COUNT, new Object[] { newsId });
	}

	public void save(ResFile ResImage) {
		dao.save(ResImage);
	}

	public ResFile load(String id) {
		return dao.load(ResFile.class, new Long(id));
	}

	public ResFile get(String id) {
		return dao.get(ResFile.class, new Long(id));
	}

	public void delete(String id) {
		dao.delete(this.get(id));
	}

	public void updateUp(String id) {
		ResFile pe = this.get(id);
		ResFile peUp = findUp(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}

	public void updateDown(String id) {
		ResFile pe = this.get(id);
		ResFile peUp = findDown(pe);
		if (peUp != null) {
			switchPosion(pe, peUp);
		}
	}

	private void switchPosion(ResFile pe, ResFile peUp) {
		long pe1idx = pe.getIdx();
		pe.setIdx(peUp.getIdx());
		peUp.setIdx(pe1idx);
		dao.save(pe);
		dao.save(peUp);
	}

	private ResFile findUp(ResFile pe) {
		return dao.findSingle(ResFile.class, QL.HQL.ResFile.LIST_UP,
				new Object[] { pe.getResId(),pe.getIdx() });
	}

	private ResFile findDown(ResFile pe) {
		return dao.findSingle(ResFile.class, QL.HQL.ResFile.LIST_DOWN,
				new Object[] {pe.getResId(),pe.getIdx() });
	}

	public Long getSeq(Long newsId) {
		return dao.getSeq(QL.HQL.ResFile.GET_MAX_IDX, new Object[] {newsId});
	}
}
